﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace iMKelly {
    using CoreClass;
    public partial class frmBookManagement : Form {
        private CoreClass.Database dbs;
        public frmBookManagement() {
            InitializeComponent();
        }
        public frmBookManagement(CoreClass.Database dbs) {
            InitializeComponent();
            this.dbs = dbs;
            RefreshViewer();   
        }

        private void button1_Click(object sender, EventArgs e) {
            frmImport frmimport = new frmImport(dbs);
            frmimport.MdiParent = this.MdiParent;
            frmimport.Show();
            frmimport.Complete += new CompleteHandler(onComplete);
        }

        void onComplete() {
            RefreshViewer();
        }

        private void modify_Click(object sender, EventArgs e) {
            DataGridViewRow row = viewer.CurrentRow;
            BookTitle title = new BookTitle(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString(), row.Cells[2].Value.ToString(), row.Cells[3].Value.ToString(), Convert.ToInt16(row.Cells[4].Value.ToString()));
            frmImport modify = new frmImport(dbs, title);
            modify.Text = "Modify " + title.TitleID;
            modify.MdiParent = this.MdiParent;
            modify.Complete += new CompleteHandler(onComplete);
            modify.Show();
        }

        private void RefreshViewer() {
            viewer.Rows.Clear();
            BookTitle[] lst = dbs.findBookByName("");
            foreach (BookTitle t in lst) {
                viewer.Rows.Add(new String[] { t.TitleID, t.Name, t.Author, t.Publisher, t.Amount.ToString() });
            }
        }

        private void remove_Click(object sender, EventArgs e) {
            DataGridViewRow row = viewer.CurrentRow;
            BookTitle title = new BookTitle(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString(), row.Cells[2].Value.ToString(), row.Cells[3].Value.ToString(), Convert.ToInt16(row.Cells[4].Value.ToString()));
            if (MessageBox.Show("Do you really want to wipe it out?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == global::System.Windows.Forms.DialogResult.Yes) {
                dbs.removeBookTitle(title.TitleID);
                RefreshViewer();
            }
        }
    }
}
